using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UCPipe : Control
    {
        private PipeDirection direction = PipeDirection.Horizontal;
        private bool isFlow = false;
        private bool isDashed = false;

        public enum PipeDirection
        {
            Horizontal,
            Vertical,
            UpRight,
            RightDown
        }

        [Category("外观")]
        [Description("管道方向")]
        public PipeDirection Direction
        {
            get { return direction; }
            set 
            {
                direction = value;
                this.Invalidate();
            }
        }

        [Category("行为")]
        [Description("是否有水流")]
        public bool IsFlow
        {
            get { return isFlow; }
            set 
            {
                isFlow = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("是否虚线")]
        public bool IsDashed
        {
            get { return isDashed; }
            set 
            {
                isDashed = value;
                this.Invalidate();
            }
        }

        public UCPipe()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            this.Size = new Size(100, 20);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 创建画笔
            using (Pen pen = new Pen(Color.Black, 2))
            {
                if (isDashed)
                {
                    pen.DashStyle = DashStyle.Dash;
                }

                // 根据方向绘制管道
                switch (direction)
                {
                    case PipeDirection.Horizontal:
                        g.DrawLine(pen, 5, this.Height / 2, this.Width - 5, this.Height / 2);
                        break;
                    case PipeDirection.Vertical:
                        g.DrawLine(pen, this.Width / 2, 5, this.Width / 2, this.Height - 5);
                        break;
                    case PipeDirection.UpRight:
                        g.DrawLine(pen, 5, this.Height - 5, this.Width - 5, 5);
                        break;
                    case PipeDirection.RightDown:
                        g.DrawLine(pen, 5, 5, this.Width - 5, this.Height - 5);
                        break;
                }
            }

            // 如果有水流，绘制箭头
            if (isFlow)
            {
                using (Pen pen = new Pen(Color.Blue, 2))
                {
                    DrawFlowArrow(g, pen);
                }
            }
        }

        private void DrawFlowArrow(Graphics g, Pen pen)
        {
            PointF[] arrowPoints = new PointF[3];
            float arrowSize = 8;

            switch (direction)
            {
                case PipeDirection.Horizontal:
                    arrowPoints[0] = new PointF(this.Width / 2 + 10, this.Height / 2);
                    arrowPoints[1] = new PointF(this.Width / 2, this.Height / 2 - arrowSize / 2);
                    arrowPoints[2] = new PointF(this.Width / 2, this.Height / 2 + arrowSize / 2);
                    break;
                case PipeDirection.Vertical:
                    arrowPoints[0] = new PointF(this.Width / 2, this.Height / 2 + 10);
                    arrowPoints[1] = new PointF(this.Width / 2 - arrowSize / 2, this.Height / 2);
                    arrowPoints[2] = new PointF(this.Width / 2 + arrowSize / 2, this.Height / 2);
                    break;
                case PipeDirection.UpRight:
                    arrowPoints[0] = new PointF(this.Width / 2 + 5, this.Height / 2 - 5);
                    arrowPoints[1] = new PointF(this.Width / 2 - arrowSize / 2, this.Height / 2 + arrowSize / 2);
                    arrowPoints[2] = new PointF(this.Width / 2 + arrowSize / 2, this.Height / 2 + arrowSize / 2);
                    break;
                case PipeDirection.RightDown:
                    arrowPoints[0] = new PointF(this.Width / 2 + 5, this.Height / 2 + 5);
                    arrowPoints[1] = new PointF(this.Width / 2 - arrowSize / 2, this.Height / 2 - arrowSize / 2);
                    arrowPoints[2] = new PointF(this.Width / 2 + arrowSize / 2, this.Height / 2 - arrowSize / 2);
                    break;
            }

            g.FillPolygon(pen.Brush, arrowPoints);
        }
    }
}